﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Nova_DAL
{
    public class DepartMent
    {
        int i = 0;
        string strSQL = "";
        DataSet ds = null;
        DbHelper dbHelper = new DbHelper();


        ///<summary>
        ///获取部门列表
        ///</summary>
        ///<param name="sQL">SQL</param>
        ///<returns>返回DataSet</returnss>
        ///Writer：[王成友]
        ///Create Date： [2010-5-20]
        ///</summary>
        public DataSet DataListvDepartment()
        {
            try
            {
                strSQL = "select PID,vDepartmentNo,vDepartment from tbDepart order by PID";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);

            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }

            return ds;
        }

        /// <summary>
        /// 获取部门详细信息，根据id
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>返回DataSet</returns>
        ///Writer：[夏鑫]
        ///Create Date： [2010-5-26]
        ///</summary>
        public DataSet DataListvDepartment(string vDepartmentNo)
        {
            try
            {
                strSQL = "select PID,vDepartmentNo,vDepartment from tbDepart where vDepartmentNo='" + vDepartmentNo + "'";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }

            return ds;
        }

        /// <summary>
        /// 获取部门详细信息，根据编号
        /// <param name="id">id 0:添加</param>
        /// <param name="strvDepartmentNo">编号</param>
        /// <returns>返回DataSet</returns>
        /// Writer：[夏鑫]
        /// Create Date： [2010-5-27]
        /// </summary>
        public DataSet DataListvDepartment(string PID, string strvDepartmentNo)
        {
            try
            {
                if (PID == "0")
                {
                    strSQL = "select PID from tbDepart where vDepartmentNo='" + strvDepartmentNo + "'";
                }
                else
                {
                    strSQL = "select PID from tbDepart where vDepartmentNo='" + strvDepartmentNo + "' and PID<>'" + PID + "'";
                }
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }

            return ds;
        }

        ///<summary>
        ///增加部门
        ///</summary>
        ///<param name="sQL">vDepartmentNo,vDepartment</param>
        ///<returns>返回int,0失败,1成功</returnss>
        ///Writer：[王成友]
        ///Create Date： [2010-5-20]
        ///</summary>
        public int AddDepartMent(Nova_MOD.DepartMent DepartMent)
        {
            try
            {
                strSQL = "insert into tbDepart(vDepartmentNo,vDepartment) values(@vDepartmentNo,@vDepartment)";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@vDepartmentNo", DbType.String, DepartMent.vDepartmentNo);
                dbHelper.AddInParameter(cmd, "@vDepartment", DbType.String, DepartMent.vDepartment);
                i = dbHelper.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return i;
        }

        ///<summary>
        ///修改部门
        ///</summary>
        ///<param name="sQL">vDepartmentNo,vDepartment,id</param>
        ///<returns>返回int,0失败,1成功</returnss>
        ///Writer：[王成友]
        ///Create Date： [2010-5-20]
        ///</summary>
        public int UpdateDepartMent(Nova_MOD.DepartMent DepartMent)
        {
            try
            {
                strSQL = "update tbDepart set vDepartmentNo =@vDepartmentNo,vDepartment=@vDepartment where PID =@PID";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@vDepartmentNo", DbType.String, DepartMent.vDepartmentNo);
                dbHelper.AddInParameter(cmd, "@vDepartment", DbType.String, DepartMent.vDepartment);
                dbHelper.AddInParameter(cmd, "@PID", DbType.String, DepartMent.PID);
                i = dbHelper.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return i;
        }

        ///<summary>
        ///删除部门
        ///</summary>
        ///<param name="sQL">id</param>
        ///<returns>返回int,0失败,1成功</returnss>
        ///Writer：[王成友]
        ///Create Date： [2010-5-20]
        ///</summary>
        public int DeleteDepartMent(string PID)
        {
            try
            {
                strSQL = "delete from tbDepart where PID =@PID";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@PID", DbType.String, PID);
                i = dbHelper.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return i;
        }

        /// <summary>
        /// 删除多个部门
        /// </summary>
        /// <param name="strID">部门id</param>
        /// <returns>返回int,0失败,1成功</returns>
        /// Writer:[夏鑫]
        /// Create Date:[2010-5-28]
        public int DeleteAllDepartMent(string strID)
        {
            try
            {
                strSQL = "delete from tbDepart where PID in  (" + strID + ") ";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                i = dbHelper.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return i;
        }
    }
}

